﻿using System;
using System.Windows.Forms;
using Projet_Synthese;
using System.Diagnostics;
using System.IO;

namespace Projet_Synthese_Editeur
{
    public partial class Form1 : Form
    {
        public Partie Partie;
        private String path = "";
        private String nomFile = "";
        ConvertisseurXNB convertisseur;
        public Form1()
        {
            InitializeComponent();
            this.controlXna.MainForm = this;
            convertisseur = new ConvertisseurXNB();
            Partie = new Partie();
            
        }
        public void UpdateVue()
        {

            toolStripButton2.Enabled = (Partie.IndexID > 0);
            toolStripButton3.Enabled = (Partie.Count > (Partie.IndexID + 1));
            toolStripLabel2.Text = (Partie.IndexID + 1).ToString() + " / " + Partie.Count;
            toolStripTextBox1.Text = Partie.Tangram.Nom;
            toolStripButton1.Enabled = Partie.Count > 1;
            UpdateOptionStatuts();
           
        }
        private void quitterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void ouvrirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.FilterIndex = 1;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                ExtraitrePath(openFileDialog1.FileName);
                Partie = Outils.Load(path +"\\"+ nomFile + ".xml", controlXna.Textures,true);
                UpdateVue();
            }
        }

        private void enregistrerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Partie != null)
            {
                if (path.Trim() == "" || nomFile.Trim() == "")
                    enregistrersousToolStripMenuItem_Click(sender, e);
                else Outils.Save(path + "\\" + nomFile + ".xml", Partie);
            }
        }
        private void ExtraitrePath(String nom)
        {
            int debutNom = nom.LastIndexOf('\\');
            int finNom = nom.LastIndexOf('.');
            path = nom.Substring(0, debutNom);
            nomFile = nom.Substring(debutNom + 1, finNom - debutNom - 1);
        }
        private void enregistrersousToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Partie != null && saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                ExtraitrePath(saveFileDialog1.FileName);
                Partie.Nom = nomFile;
                Outils.Save(path+"\\"+nomFile+".xml", Partie);
            }
        }

        private void nouveauToolStripMenuItem_Click(object sender, EventArgs e)
        {
            nomFile = "";
            Partie.Tangrams.Clear();
            Partie.Tangrams.Add(new Tangram(controlXna.Textures));
        }

        private void nouveauToolStripButton_Click(object sender, EventArgs e)
        {
            Partie.Tangram.Initialisation();
        }

        private void enregistrerToolStripButton_Click(object sender, EventArgs e)
        {
                String nom  = null;
                if (!verifierNom(out nom))
                    return;
                Partie.Tangram.Nom = nom;

                if ((Partie.IndexID+1)==Partie.Count)
                {
                    Partie.Tangrams.Add(new Tangram(controlXna.Textures));
                    Partie.MoveNext();
                    UpdateVue();
                }
        }

        private bool verifierNom(out String nom)
        {
            nom = toolStripTextBox1.Text.Trim();
            if (nom == "")
            {
                MessageBox.Show("Vous devez donner un nom à votre forme", "Erreur : champ nom vide", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            String nom = null;
            if (!verifierNom(out nom))
                return;
            Partie.Tangram.Nom = nom;
            Partie.MovePrev();
            UpdateVue();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            String nom = null;
            if (!verifierNom(out nom))
                return;
            Partie.Tangram.Nom = nom;
            Partie.MoveNext();
            UpdateVue();
            
        }

        public void UpdateOptionStatuts(FormeGeometrique form)
        {
            if (form != null)
            {
                if(form.IgnorePlacement)
                    ignorePlacement.CheckState = CheckState.Checked;
                else ignorePlacement.CheckState = CheckState.Unchecked;
                if (form.IgnoreRotation)
                    ignoreRotation.CheckState = CheckState.Checked;
                else ignoreRotation.CheckState = CheckState.Unchecked;
            }
        }

        public void UpdateOptionStatuts()
        {
            if (Partie.Tangram.AssistanceRotation)
                rotationToolStripMenuItem.CheckState = CheckState.Checked;
            else rotationToolStripMenuItem.CheckState = CheckState.Unchecked;
            if (Partie.Tangram.AssistanceDeplacement)
                deplacementToolStripMenuItem.CheckState = CheckState.Checked;
            else deplacementToolStripMenuItem.CheckState = CheckState.Unchecked;
        }
        private void toolStripTextBox1_MouseLeave(object sender, EventArgs e)
        {
            Partie.Tangram.Nom = toolStripTextBox1.Text;
        }

        private void onClickIgnoreRotation(object sender, EventArgs e)
        {
            ToolStripMenuItem iteam = sender as ToolStripMenuItem;
            if (iteam != null && Partie.Tangram[controlXna.Selection] != null)
            {
                Partie.Tangram[controlXna.Selection].IgnoreRotation = !iteam.Checked;
                UpdateOptionStatuts(Partie.Tangram[controlXna.Selection]);
                Partie.Tangram[controlXna.Selection].EstSelectionné = false;
                controlXna.Selection = -1;
            }
        }

        private void onClickIgnorePlacement(object sender, EventArgs e)
        {
            ToolStripMenuItem iteam = sender as ToolStripMenuItem;
            if (iteam != null && Partie.Tangram[controlXna.Selection] != null)
            {
                Partie.Tangram[controlXna.Selection].IgnorePlacement = !iteam.Checked;
                UpdateOptionStatuts(Partie.Tangram[controlXna.Selection]);
                Partie.Tangram[controlXna.Selection].EstSelectionné = false;
                controlXna.Selection = -1;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (Partie.Delete())
                UpdateVue();
        }
        public void deplacerFichier(String source,String destination,String nom)
        {
            string sourceFile = source + "\\"+nom;
            string destinationFile = destination + "\\" + nom;

            if(!System.IO.File.Exists(destinationFile))
                System.IO.File.Move(sourceFile, destinationFile);
        }
        private void imageDeFondToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.FilterIndex = 3;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (convertisseur.Conversion(openFileDialog1.FileNames, "Temp"))
                {
                    Partie.ImageFond = Path.GetFileNameWithoutExtension(openFileDialog1.FileNames[0]);
                    deplacerFichier(Directory.GetCurrentDirectory() + "\\Temp", Directory.GetCurrentDirectory() + "\\Textures", Partie.ImageFond + ".xnb");
                }
                else afficherEchec();
            }
        }

        private void sonDeposéToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.FilterIndex = 2;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (convertisseur.Conversion(openFileDialog1.FileNames, "Temp"))
                {
                    Partie.EffetDeposé = Path.GetFileNameWithoutExtension(openFileDialog1.FileNames[0]);
                    deplacerFichier(Directory.GetCurrentDirectory() + "\\Temp", Directory.GetCurrentDirectory() + "\\Textures", Partie.EffetDeposé + ".xnb");
                }
                else afficherEchec();
            }
        }
        private void afficherEchec()
        {
            MessageBox.Show("Echec de conversion", "La conversion a echoué", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        private void sonAmbianceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.FilterIndex = 2;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (convertisseur.Conversion(openFileDialog1.FileNames, "Temp", false))
                {
                    Partie.Ambiance = Path.GetFileNameWithoutExtension(openFileDialog1.FileNames[0]);
                    deplacerFichier(Directory.GetCurrentDirectory() + "\\Temp", Directory.GetCurrentDirectory() + "\\Textures", Partie.Ambiance + ".xnb");
                    deplacerFichier(Directory.GetCurrentDirectory() + "\\Temp", Directory.GetCurrentDirectory() + "\\Textures", Partie.Ambiance + ".wma");
                }
                else afficherEchec();
            }
        }

        private void sonToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.FilterIndex = 2;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (convertisseur.Conversion(openFileDialog1.FileNames, "Temp"))
                {
                    Partie.EffetTerminé = Path.GetFileNameWithoutExtension(openFileDialog1.FileNames[0]);
                    deplacerFichier(Directory.GetCurrentDirectory() + "\\Temp", Directory.GetCurrentDirectory() + "\\Textures", Partie.EffetTerminé + ".xnb");
                }
                else afficherEchec();
            }
        }

        private void rotationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Partie.Tangram.AssistanceRotation = !rotationToolStripMenuItem.Checked;
            UpdateOptionStatuts();
        }

        private void deplacementToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Partie.Tangram.AssistanceDeplacement = !deplacementToolStripMenuItem.Checked;
            UpdateOptionStatuts();
        }
    }
}

